package com.itheima.prize.commons.utils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.itheima.prize.commons.constant.SystemConstants;
import com.itheima.prize.commons.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

@ApiModel("响应报文")
@Data
public class ApiResult<T> {

    @ApiModelProperty(value = "状态码（-1=异常，0=业务不成功，1=业务成功）", required = true)
    private String code;

    @ApiModelProperty(value = "信息", required = true)
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "服务器时间")
    @DateTimeFormat(pattern = "yyyy/MM/dd hh:mm:ss")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
    private Date now = new Date();

    // 私有化构造方法，强制使用静态工厂方法
    private ApiResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return new ApiResult<>(ResultCodeEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ResultCodeEnum.SUCCESS.getCode(), SystemConstants.OPERATION_SUCCESS, data);
    }

    public static <T> ApiResult<T> error(String msg, T data) {
        return new ApiResult<>(ResultCodeEnum.FAIL.getCode(), msg, data);
    }

    public static <T> ApiResult<T> error(String msg) {
        return new ApiResult<>(ResultCodeEnum.FAIL.getCode(), msg, null);
    }

    public static <T> ApiResult<T> Failure(String msg, T data) {
        return new ApiResult<>(ResultCodeEnum.Failure.getCode(), msg, data);
    }

    public static <T> ApiResult<T> Failure(String msg) {
        return new ApiResult<>(ResultCodeEnum.Failure.getCode(), msg, null);
    }

    public static <T> ApiResult<T> successStatus(String msg, T data) {
        return new ApiResult<>(ResultCodeEnum.SUCCESS_STATUS.getCode(), msg, data);
    }

    public static <T> ApiResult<T> successStatus(T data) {
        return new ApiResult<>(ResultCodeEnum.SUCCESS_STATUS.getCode(), SystemConstants.OPERATION_SUCCESS, data);
    }


}
